﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models.BLL;
using tracker.Models;
using System.Data;
using tracker.Models.DTO;

namespace tracker.Controllers
{
   public class HandlerSearchTrackerNameController : Controller
    {
       /// <summary>
       /// Get All Alarm Define
       /// </summary>
       /// <returns></returns>
        [CallAjaxFilter]
        public JsonStringResult Index()
        {
            string result = "{[]}";
            string _username = HttpContext.Session[Common.USER].ToString();
            string _language = HttpContext.Session[Common.LANGUAGE].ToString();
            _language = _language == string.Empty ? "vi" : _language;

            List<ENTTrackerName> list = new List<ENTTrackerName>();
            list = getListDeviceByUser(_username);

            if (list.Count > 0)
            {
                result = Utilities.getJsonString(list, "data:", 1);
                list = null;
            }
            else
            {
                result = "{[]}";
            }

            return new JsonStringResult(result);  
        }

        public static List<ENTTrackerName> getListDeviceByUser(string _username) 
        {
            List<ENTTrackerName> list = new List<ENTTrackerName>();

            // Get information of current user
            DataTable dtGetUser = UserLoginBLL.SelectByUserName(new string[] { EUserLogin.FIELD_USERNAME },
                                                                new object[] { _username }).Tables[0];
            if (dtGetUser.Rows.Count > 0)
            {
                DataTable dtParent = null;

                if (dtGetUser.Rows[0][EUserLogin.FIELD_USERGROUPID].ToString() == "3")
                {
                    dtParent = UserLoginBLL.SelectAllUserByAdmin().Tables[0];
                }
                else if (dtGetUser.Rows[0][EUserLogin.FIELD_USERGROUPID].ToString() == "4")
                {
                    dtParent = UserLoginBLL.SelectAllUserByParent(new string[] { EUserLogin.FIELD_PARENTCODE },
                                                                  new object[] { dtGetUser.Rows[0][EUserLogin.FIELD_SECURITYCODE].ToString() }).Tables[0];
                }
                else
                {
                    dtParent = UserLoginBLL.SelectAllUserByParent(new string[] { EUserLogin.FIELD_USERNAME },
                                                                  new object[] { dtGetUser.Rows[0][EUserLogin.FIELD_USERNAME].ToString() }).Tables[0];

                }

                foreach (DataRow itemparent in dtParent.Rows)
                {
                    DataTable dtGetDevice = DeviceBLL.SelectAllByUserID(new string[] { EUserLogin.FIELD_USERID },
                                                                        new object[] { int.Parse(itemparent["UserID"].ToString()) }).Tables[0];
                    foreach (DataRow item in dtGetDevice.Rows)
                    {
                        ENTTrackerName entTrackername = new ENTTrackerName();
                        entTrackername.trackname = item["DeviceName"].ToString();
                        entTrackername.username = item["Username"].ToString();
                        entTrackername.snid = item["DeviceKeySecurity"].ToString();
                        list.Add(entTrackername);
                    }
                    dtGetDevice.Dispose();
                }
                dtParent.Dispose();
            }

            return list;
        }
    }
}
